package com.xmb.wechat.view.wechat.chat;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.blankj.utilcode.util.KeyboardUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.widget.TitleLayout;

import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/9 15:31
 */
public class WechatPYQAddLikesActivity extends BaseActivity {

    @BindView(R2.id.title_layout)
    TitleLayout mTitleLayout;
    @BindView(R2.id.et)
    EditText    mEt;

    private boolean  isAdd;
    private String   mInitValue;
    private String[] mStringArray;

    public WechatPYQAddLikesActivity() {
        super(R.layout.activity_wechat_pyq_add_likes);
    }

    public static void start4Result(Activity ctx, String likes, int requestCode) {
        Intent intent = new Intent(ctx, WechatPYQAddLikesActivity.class);
        intent.putExtra("data", likes);
        ctx.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void initView() {
        mTitleLayout.setRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyboardUtils.hideSoftInput(WechatPYQAddLikesActivity.this);
                Intent intent = new Intent();
                intent.putExtra("result", mEt.getText().toString().trim());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        mStringArray = getResources().getStringArray(R.array.nickName);
        //回显
        String data = getIntent().getStringExtra("data");
        if (TextUtils.isEmpty(data)) {//新增
            isAdd = true;
        } else {//编辑
            mEt.setText(data);
            mTitleLayout.setTitle("编辑点赞");
            mInitValue = mEt.getText().toString().trim();
        }
    }

    @OnClick(R2.id.btn_add)
    public void onViewClicked() {
        if (TextUtils.isEmpty(mEt.getText().toString().trim())) {
            mEt.append(mStringArray[new Random().nextInt(100)]);
        } else {
            mEt.append("," + mStringArray[new Random().nextInt(100)]);
        }
    }

    @Override
    protected boolean isNeedShowLeaveHint() {
        return isAdd ? !TextUtils.isEmpty(mEt.getText().toString().trim()) : !TextUtils.equals(mEt.getText().toString().trim(), mInitValue);
    }
}
